home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland Pascal with Objects 7.0 / PAINT.ZIP / TOOLBAR.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1992-10-27  |  4.0 KB  |  131 lines

  1. {************************************************}
  2. {                                                }
  3. {   ObjectWindows Paint demo                     }
  4. {   Copyright (c) 1992 by Borland International  }
  5. {                                                }
  6. {************************************************}
  7.  
  8. unit ToolBar;
  9.  
  10. { This unit defines a tool bar window for the paint program.
  11.   The toolbar is responsible for the management of the available tools
  12.   and displays the icons for the available tools. Selection of the current
  13.   tool is handled here.
  14. }
  15.  
  16. interface
  17.  
  18. uses PaintDef, Tools, WinTypes, WinProcs, OWindows;
  19.  
  20. type
  21.   { All available tools }
  22.   ToolName = (PenTool, LineTool,
  23.               FillTool,                    { ordering defines the layout of }
  24.               ORectTool, FRectTool,     { the display of icons }
  25.               OEllipseTool, FEllipseTool,
  26.               EraserTool, SelectTool);
  27.  
  28. const
  29.   MinTool = PenTool;
  30.   MaxTool = SelectTool;
  31.  
  32. type
  33.   PToolBar = ^TToolBar;
  34.   TToolBar = object(TWindow)
  35.     State: PState;            { communication among modules }
  36.     Tools: array[ToolName] of PPaintTool; { tools available }
  37.  
  38.     { Creation and destruction }
  39.     constructor Init(AParent: PWindowsObject; AState: PState);
  40.     destructor Done; virtual;
  41.  
  42.     { Utility }
  43.     procedure ToolSelect(Tool: ToolName);
  44.  
  45.     { Display } 
  46.     procedure Paint(PaintDC: HDC; var PaintInfo: TPaintStruct); virtual;
  47.  
  48.     { Window manager responses }
  49.     procedure WMLButtonDown(var Msg: TMessage);
  50.       virtual wm_First + wm_LButtonDown;
  51.   end;
  52.  
  53. implementation
  54.  
  55. { Create the actual toolbar and a new instance of each tool it is to contain.
  56. }
  57. constructor TToolBar.Init(AParent: PWindowsObject; AState: PState);
  58. begin
  59.   TWindow.Init(AParent, nil);
  60.   Attr.Style := ws_Child or ws_Visible;
  61.   State := AState;
  62.   Tools[PenTool] := New(PPenTool, Init(AState, 'PenTool', 'PenCursor'));
  63.   Tools[LineTool] := New(PLineTool, Init(AState, 'LineTool', 'PenCursor', False));
  64.   Tools[FillTool] := New(PFillTool, Init(AState, 'FillTool', 'FillCursor'));
  65.   Tools[ORectTool] := New(PRectTool, Init(AState, 'RectTool', 'CrossCursor', False));
  66.   Tools[FRectTool] := New(PRectTool, Init(AState, 'FillRectTool', 'CrossCursor', True));
  67.   Tools[OEllipseTool] := New(PEllipseTool, Init(AState, 'EllipseTool', 'CrossCursor', False));
  68.   Tools[FEllipseTool] := New(PEllipseTool, Init(AState, 'FillEllipseTool', 'CrossCursor', True));
  69.   Tools[EraserTool] := New(PEraserTool, Init(AState, 'EraserTool', 'EraserCursor'));
  70.   Tools[SelectTool] := New(PSelectTool, Init(AState, 'SelectTool', 'CrossCursor', False));
  71.   Tools[PenTool]^.Select;
  72. end;
  73.  
  74. { Destroy each tool instance befory dying.
  75. }
  76. destructor TToolBar.Done;
  77. var
  78.   Tool: ToolName;
  79. begin
  80.   for Tool := MinTool to MaxTool do Dispose(Tools[Tool], Done);
  81.   TWindow.Done;
  82. end;
  83.  
  84. { Deselect the current tool and select the new tool and update the display.
  85. }
  86. procedure TToolBar.ToolSelect(Tool: ToolName);
  87. begin
  88.   State^.PaintTool^.Deselect;
  89.   Tools[Tool]^.Select;
  90.   InvalidateRect(HWindow, nil, False);
  91. end;
  92.  
  93. { Paint the toolbar by painting the icon for each tool horizontally. The icon
  94.   for the currently selected tool is highlighted. Note that icons are 32x32,
  95.   but overlap by one pixel.
  96. }
  97. procedure TToolBar.Paint(PaintDC: HDC; var PaintInfo: TPaintStruct);
  98. var
  99.   I: Integer;        { Position of tool in row of icons }
  100.   Tool: ToolName;    { Current tool being drawn }
  101.   R: TRect;        { Coordinates of icon }
  102. begin
  103.   for Tool := MinTool to MaxTool do
  104.   begin
  105.     I := Ord(Tool);
  106.     DrawIcon(PaintDC, I * 31, 0, Tools[Tool]^.Icon);
  107.     
  108.     { Highlight currently selected tool }
  109.     if Tools[Tool] = State^.PaintTool then
  110.     begin
  111.       R.top := 1;
  112.       R.left := I * 31 + 1;
  113.       R.bottom := R.top + 30;
  114.       R.right := r.left + 30;
  115.       InvertRect(PaintDC, R);
  116.     end;
  117.   end;
  118. end;
  119.  
  120. { Select the tool whose icon is pressed.
  121. }
  122. procedure TToolBar.WMLButtonDown(var Msg: TMessage);
  123. var
  124.   Tool: ToolName;
  125. begin
  126.   Tool := ToolName(Msg.LParamLo div 31);
  127.   if Tool <= MaxTool then ToolSelect(Tool);
  128. end;
  129.  
  130. end.
  131.